package svgedit.japangui.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;

import svgedit.gui.SVGFileFilter;
import svgedit.japangui.JapanFrame;

/** Shows discard changes dialog, then presents a file dialog for the
 *  user to select an existing document to edit.
 */
@SuppressWarnings("serial")
public class OpenDocumentAction extends AbstractAction {

    private JapanFrame japanFrame;

    /** Create this action for the given frame */
    public OpenDocumentAction(JapanFrame japanFrame) {
        super("\u958B\u304F...");
        this.japanFrame = japanFrame;
    }

    public void actionPerformed(ActionEvent ae) {
        if (japanFrame.confirmSaveChanges()) {
            String directory = japanFrame.getPreferences().getDefaultPath();
            JFileChooser dialog = new JFileChooser(directory);
            dialog.addChoosableFileFilter(new SVGFileFilter());
            dialog.showOpenDialog(null);

            File file = dialog.getSelectedFile();
            if (file != null) {
            	japanFrame.getPreferences().setDefaultPath(file.getPath());
            	japanFrame.openFile(file);
            }
        }
    }


}
